package com.ningxia.water.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ningxia.water.entity.TsMeter;
import com.ningxia.water.entity.TsMeterType;
import com.ningxia.water.entity.TsWellMeter;
import com.ningxia.water.enums.MeterStatusEnum;
import com.ningxia.water.mapper.TsMeterFlowMapper;
import com.ningxia.water.mapper.TsMeterMapper;
import com.ningxia.water.mapper.TsMeterTypeMapper;
import com.ningxia.water.reuslt.Result;
import com.ningxia.water.reuslt.ResultResponse;
import com.ningxia.water.service.ITsMeterFlowService;
import com.ningxia.water.service.ITsMeterService;
import com.ningxia.water.service.ITsMeterTypeService;
import com.ningxia.water.service.ITsWellMeterService;
import com.ningxia.water.utils.CommonUtils;
import com.ningxia.water.vo.out.MeterStatusCountsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangzhen
 * @create 2021-03-17 11:34
 * 设备的CURD
 */
@RestController
@RequestMapping("/meter")
@Api(tags = "基本信息-机井中设备的api")
public class MeterController {

    @Autowired
    private ITsMeterService meterService;

    @Autowired
    private ITsMeterFlowService meterFlowService;

    @Autowired
    private ITsMeterTypeService meterTypeService;

    @PostMapping("/insertmeterinfo")
    @ApiOperation(value = "新增机井中的设备")
    public Result insertMeterInfo(@RequestBody TsMeter meter, @RequestParam("wellId") Integer wellId) {
        return CommonUtils.commonResult(meterService.insertMeterInfo(meter,wellId));
    }

    @PutMapping("/updatemeterinfo")
    @ApiOperation(value = "根据设备ID更新设备")
    public Result updateMeterInfo(@RequestBody TsMeter meter) {
        return CommonUtils.commonResult(meterService.updateById(meter));
    }

    @GetMapping("/getallmetertypes")
    @ApiOperation(value = "获取全部的设备类型列表")
    public Result getAllMeterTypes() {
        //获取所有设备类型
        return ResultResponse.getSuccessResult(meterTypeService.list());
    }


    @DeleteMapping("/deletemeterinfo")
    @ApiOperation(value = "根据设备ID删除设备")
    public Result deleteMeterInfo(@RequestParam("meterId") Integer meterId) {
        Integer count = meterFlowService.checkData(meterId);
        if (count > 0) {
            return ResultResponse.getFailResult("此设备存在数据，无法删除！");
        } else {
            // 刪除 设备表里的 meter 的同时 删除关联表中的meter记录
            return CommonUtils.commonResult(meterService.deleteMeter(meterId));
        }
    }

}
